<template>
    <div class="edit-popup">
        <popup
                ref="popupRef"
                :title="popupTitle"
                :async="true"
                width="550px"
                @confirm="handleSubmit"
                @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="164px" :rules="formRules">
                <el-form-item label="企业名称:" prop="comName">
                    <el-input
                            v-model="formData.comName"
                            placeholder="请输入企业名称"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="法定代表人:" prop="comLeader">
                    <el-input
                            v-model="formData.comLeader"
                            placeholder="请输入法定代表人"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="曾用名:" prop="comOldname">
                    <el-input
                            v-model="formData.comOldname"
                            placeholder="请输入曾用名"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="企业代码:" prop="comCode">
                    <el-input
                            v-model="formData.comCode"
                            placeholder="请输入企业代码"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="注册资金:" prop="comFund">
                    <el-input
                            v-model="formData.comFund"
                            placeholder="请输入注册资金"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="企业地址:" prop="comAddress">
                    <el-input
                            v-model="formData.comAddress"
                            placeholder="请输入企业地址"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="经营状况:" prop="comStatus">
                    <el-select  v-model="formData.comStatus"  style="width: 100%" >
                        <el-option label="正常" value=1 />
                        <el-option label="异常" value=2 />

                    </el-select>
                </el-form-item>
                <el-form-item label="企业类型:" prop="comType">
                    <el-input
                            v-model="formData.comType"
                            placeholder="请输入企业类型"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="所属行业:" prop="comIndustry">
                    <el-input
                            v-model="formData.comIndustry"
                            placeholder="请输入所属行业"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="成立日期:" prop="comBirthdate">
                    <el-date-picker
                            v-model="formData.comBirthdate"
                            type="date"
                            placeholder="请选择成立日期"
                            format="YYYY-MM-DD"
                            value-format="YYYY-MM-DD"
                    />
                </el-form-item>
                <el-form-item label="所属省:" prop="comProvince">
                    <el-input
                            v-model="formData.comProvince"
                            placeholder="请输入所属省"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="所属市:" prop="comCity">
                    <el-input
                            v-model="formData.comCity"
                            placeholder="请输入所属市"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="所属区:" prop="comArea">
                    <el-input
                            v-model="formData.comArea"
                            placeholder="请输入所属区"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="经营范内:" prop="comScope">
                    <el-input
                            v-model="formData.comScope"
                            placeholder="请输入经营范内"
                            :maxlength="100"
                    />
                </el-form-item>
                <el-form-item label="最后更新时间:" prop="lastUpdateTime">
                    <el-date-picker
                            v-model="formData.lastUpdateTime"
                            type="date"
                            placeholder="请选择最后更新时间"
                            format="YYYY-MM-DD"
                            value-format="YYYY-MM-DD"
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
    import type { FormInstance } from 'element-plus'
    import { combaseEdit, combaseAdd,  } from '@/api/combase/combase'
    import Popup from '@/components/popup/index.vue'
    const emit = defineEmits(['success', 'close'])
    const formRef = shallowRef<FormInstance>()
    const popupRef = shallowRef<InstanceType<typeof Popup>>()
    const mode = ref('add')
    const popupTitle = computed(() => {
        return mode.value == 'edit' ? '编辑基础线索' : '新增基础线索'
    })
    const formData = reactive({
        comName: '',
        comLeader: '',
        comOldname:'',
        comCode: '',
        comFund: '',
        comAddress:'',
        comStatus: '',
        comIndustry: '',
        comProvince:'',
        comCity: '',
        comArea: '',
        comType: '',
        lastUpdateTime:'',
        comScope: '',
        comLeader: '',
        comBirthdate:"",
        id:''
    })

    const checkMobile = (rule: any, value: any, callback: any) => {
        if (!value) {
            return callback()
        } else {
            const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
            console.log(reg.test(value))
            if (reg.test(value)) {
                callback()
            } else {
                return callback(new Error('请输入正确的手机号'))
            }
        }
    }
    const formRules = {

        comName: [
            {
                required: true,
                message: '请输入企业名称',
                trigger: ['blur']
            }
        ],
        comLeader: [
            {
                required: true,
                message: '请输入法定代表人',
                trigger: ['blur']
            }
        ],
        comCode: [
            {
                required: true,
                message: '请输入企业代码',
                trigger: ['blur']
            }
        ],comStatus: [
            {
                required: true,
                message: '请选择经营状况',
                trigger: ['blur']
            }
        ],
    }



    const handleSubmit = async () => {
        await formRef.value?.validate()
        mode.value == 'edit' ? await combaseEdit(formData) : await combaseAdd(formData)
        popupRef.value?.close()
        emit('success')
    }

    const open = (type = 'add') => {
        mode.value = type
        popupRef.value?.open()
    }

    const setFormData = (data: Record<any, any>) => {
        Object.keys(formData).forEach(key => {
            if (data[key] != null && data[key] != undefined) {
                formData[key] = data[key]
                formData[key] = data[key]
            }
        })
        formData.isKey=formData.isKey+''
        console.log(formData)

    }

    const getDetail = async (row: Record<string, any>) => {
        const data = await deptDetail({
            id: row.id
        })
        setFormData(data)
    }

    const handleClose = () => {
        emit('close')
    }

    defineExpose({
        open,
        setFormData,
        getDetail
    })
</script>
